package com.yuantu.judiciary.utils;

import com.alibaba.fastjson.JSON;
import com.yuantu.judiciary.model.api.CompanyAdminPunishQueryDTO;
import com.yuantu.judiciary.model.api.PersonBadRecordDTO;
import com.yuantu.judiciary.model.dto.CaseCountDTO;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * 加密
 *
 * @author syw
 * @since 2023-06-29
 **/
public class AesUtil {

    /**
     * 算法
     */
    private static final String ALGORITHM_AES = "AES/ECB/PKCS5Padding";


    /**
     * base 64 encode
     *
     * @param bytes 待编码的bytel
     * @return 编码后的 base 64 code
     */
    public static String base64Encode(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    /**
     * base 64 decode
     *
     * @param base64Code 待解码的 base 64 code
     * @return 解码后的 byte[]
     * @throws Exception .
     */
    public static byte[] base64Decode(String base64Code) throws Exception {
        return StringUtils.isNotEmpty(base64Code) ? new BASE64Decoder().decodeBuffer(base64Code) : null;
    }

    /**
     * AES 加密
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的 byte[]
     * @throws Exception .
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * AES 加密为 base 64 code
     *
     * @param content    待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的 base 64 code
     * @throws Exception .
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * AES 解密
     *
     * @param encryptBytes 待解密的 byte[]
     * @param decryptKey   解密密钥
     * @return 解密后的 String
     * @throws Exception .
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    /**
     * 将 base 64 code AES 解密
     *
     * @param encryptStr 待解密的 base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的 string
     * @throws Exception .
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isNotEmpty(encryptStr) ? aesDecryptByBytes(base64Decode(encryptStr), decryptKey) : null;
    }



}
